/**
 ******************************************************************************
 * @file    rtc.c
 * @author  hyseim software Team
 * @date    15-Mar-2024
 * @brief   This file provides all the tmp functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "rtc.h"

/** @addtogroup IM110GW_Driver
 * @{
 */

/** @defgroup RTC
 * @brief RTC driver modules
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup RTC_Private_Functions
  * @{
  */

/**
 * @brief  Enable RTC counter.
 * @return None
 */
void RTC_Enable(void)
{
    RTC->CCR |= RTC_CCR_RTC_EN;
}

/**
 * @brief  Disable RTC counter.
 * @return None
 */
void RTC_Disable(void)
{
    RTC->CCR &= ~RTC_CCR_RTC_EN;
}

/**
 * @brief  Sets the RTC counter match value.
 * @param  MatchValue: RTC counter match new value.
 * @return None
 */
void RTC_SetCounterMatch(uint32_t MatchValue)
{
    RTC->CMR = MatchValue;
}

/**
 * @brief  Sets the RTC counter load value.
 * @param  LoadValue: RTC counter load new value.
 * @return None
 */
void RTC_SetCounterLoad(uint32_t LoadValue)
{
    RTC->CLR = LoadValue;
}

/**
 * @brief  Sets the RTC prescaler value.
 * @param  PrescalerValue: RTC prescaler new value.
 * @return None
 */
void RTC_SetCounterPrescaler(uint32_t PrescalerValue)
{
    RTC->CPSR = PrescalerValue;
}

/**
 * @brief  Initialize RTC.
 * @param  RTC_InitStruct: pointer to a RTC_Init_t structure
 *         that contains the configuration information for the specified 
 *         RTC peripheral.
 * @param  LoadValue: RTC counter load value.
 * @param  MatchValue: RTC counter match value.
 * @param  PrescalerValue: RTC prescaler value.
 * @return None
 */
void RTC_Init(RTC_Init_t *RTC_InitStruct, uint32_t LoadValue, uint32_t MatchValue, uint32_t PrescalerValue)
{
    RTC_SetCounterLoad(LoadValue);

    RTC_SetCounterMatch(MatchValue);

    RTC_SetCounterPrescaler(PrescalerValue);

    RTC->CCR = RTC_InitStruct->RTC_IntrEn | RTC_InitStruct->RTC_IntrMask |
               RTC_InitStruct->RTC_WrapEn | RTC_InitStruct->RTC_PsclrEn;
}

/**
 * @brief  Fills each RTC_StructInit member with its default value.
 * @param  RTC_StructInit: pointer to a RTC_Init_t structure which
 *         will be initialized.
 * @return None
 */
void RTC_StructInit(RTC_Init_t *RTC_StructInit)
{
    RTC_StructInit->RTC_IntrEn = RTC_Interrupt_Disable;
    RTC_StructInit->RTC_IntrMask = RTC_InterruptMask_Disable;
    RTC_StructInit->RTC_WrapEn = RTC_Wrap_Disable;
    RTC_StructInit->RTC_PsclrEn = RTC_Prescaler_Disable;
}

/**
 * @brief  Gets the current count value of RTC.
 * @return RTC current counter value
 */
uint32_t RTC_GetCounter(void)
{
    return RTC->CCVR;
}

/**
 * @brief  Checks whether the specified RTC interrupt has occurred or not.
 * @return The new state of the RTC_RSTAT (SET or RESET)
 */
FlagStatus_t RTC_GetITStatus(void)
{   
    ITStatus_t bitstatus = RESET;
    bitstatus = (RTC->RSTAT) & RTC_RSTAT_RSTAT;
    return bitstatus;
}

/**
 * @brief  Clears the RTC's pending flags.
 * @return None
 */
void RTC_ClearFlag(void)
{
    uint32_t eoi = RTC->EOI;
}

/**
  * @}
  */

/**
 * @}
 */

/**
 * @}
 */
